using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Stonecode.Solar.ParticleEngine;
using Stonecode.Solar.ParticleEngine.Collections;
using Stonecode.Solar.ParticleEngine.Emitters;

// TODO: replace these with the processor input and output types.
using TInput = System.Xml.XmlDocument;
//using TOutput = Stonecode.ParticleEngine.Collections.EmitterCollectionInfo;
using TOutput = System.Xml.XmlDocument
;
namespace Stonecode.Solar.ParticleEngine.Pipeline
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to apply custom processing to content data, converting an object of
    /// type TInput to TOutput. The input and output types may be the same if
    /// the processor wishes to alter data without changing its type.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    ///
    /// TODO: change the ContentProcessor attribute to specify the correct
    /// display name for this processor.
    /// </summary>
    [ContentProcessor(DisplayName = "EffectCollectionProcessor")]
    public class EffectCollectionProcessor : ContentProcessor<TInput, TOutput>
    {
        
        public override XmlDocument Process(XmlDocument input, ContentProcessorContext context)
        {
            return input;
        }

        #region Old Way
        //public override TOutput Process(TInput input, ContentProcessorContext context)
        //{
        //    TOutput output = new EmitterCollectionInfo();
        //    XmlDocument doc = input;

        //    XmlElement rootNode = doc.DocumentElement; // <XNAContent>
        //    XmlElement emitterList = (XmlElement)rootNode.ChildNodes[0]; // there should be only one list
            

        //    //System.Diagnostics.Debugger.Launch();

        //    foreach (XmlNode node in emitterList.ChildNodes) // for each emitter in the list
        //    {
        //        EmitterInfo info = new EmitterInfo();
        //        info.EmitterSettings = new EmitterSettings(uint.Parse(node["MaxParticles"].InnerText));

        //        foreach (XmlNode node2 in node.ChildNodes)
        //        {
        //            switch (node2.Name)
        //            {
        //                case "Name":
        //                    {
        //                        info.EmitterSettings.EmitterName = node2.InnerText;
        //                        break;
        //                    }
        //                case "TextureName":
        //                    {
        //                        info.EmitterSettings.TextureName = node2.InnerText;
        //                        break;
        //                    }
        //                case "EmitterTypeSettings":
        //                    {
        //                        info.EmitterTypeSettings = ParseEmitterTypeSettings((XmlElement)node2);
        //                        //info.EmitterType = info.EmitterSettings.EmitterTypeinfo.EmitterSettings.EmitterType;
        //                        break;
        //                    }
        //                case "ParticlesPerSecond":
        //                    {
        //                        info.EmitterSettings.ParticlesPerSecond = float.Parse(node2.InnerText);
        //                        break;
        //                    }
        //                case "Duration":
        //                    {
        //                        info.EmitterSettings.Duration = TimeSpan.Parse(node2.InnerText);
        //                        break;
        //                    }
        //                case "DurationRandomness":
        //                    {
        //                        info.EmitterSettings.DurationRandomness = float.Parse(node2.InnerText);
        //                        break;
        //                    }
        //                case "Enabled":
        //                    {
        //                        info.EmitterSettings.Enabled = bool.Parse(node2.InnerText);
        //                        break;
        //                    }
        //                case "Velocity":
        //                    {
        //                        info.EmitterSettings.MinHorizontalVelocity = float.Parse(node2["MinHorizontal"].InnerText);
        //                        info.EmitterSettings.MaxHorizontalVelocity = float.Parse(node2["MaxHorizontal"].InnerText);
        //                        info.EmitterSettings.MinVerticalVelocity = float.Parse(node2["MinVertical"].InnerText);
        //                        info.EmitterSettings.MaxVerticalVelocity = float.Parse(node2["MaxVertical"].InnerText);
        //                        info.EmitterSettings.EmitterVelocitySensitivity = float.Parse(node2["Sensitivity"].InnerText);
        //                        info.EmitterSettings.EndVelocity = float.Parse(node2["Falloff"].InnerText);
        //                        Vector3 gravity = Vector3.Zero;
        //                        gravity.X = float.Parse(node2["Gravity"].Attributes["X"].Value);
        //                        gravity.Y = float.Parse(node2["Gravity"].Attributes["Y"].Value);
        //                        gravity.Z = float.Parse(node2["Gravity"].Attributes["Z"].Value);
        //                        info.EmitterSettings.Gravity = gravity;
        //                        break;
        //                    }
        //                case "Color":
        //                    {
        //                        XmlElement c = node2["MinColor"];
        //                        Color co = new Color();
        //                        co.A = byte.Parse(c.Attributes["A"].Value);
        //                        co.R = byte.Parse(c.Attributes["R"].Value);
        //                        co.G = byte.Parse(c.Attributes["G"].Value);
        //                        co.B = byte.Parse(c.Attributes["B"].Value);
        //                        info.EmitterSettings.MinColor = co;

        //                        c = node2["MaxColor"];
        //                        co.A = byte.Parse(c.Attributes["A"].Value);
        //                        co.R = byte.Parse(c.Attributes["R"].Value);
        //                        co.G = byte.Parse(c.Attributes["G"].Value);
        //                        co.B = byte.Parse(c.Attributes["B"].Value);
        //                        info.EmitterSettings.MaxColor = co;
        //                        break;
        //                    }
        //                case "Rotation":
        //                    {
        //                        info.EmitterSettings.MinRotateSpeed = float.Parse(node2["MinRotateSpeed"].InnerText);
        //                        info.EmitterSettings.MaxRotateSpeed = float.Parse(node2["MaxRotateSpeed"].InnerText);
        //                        break;
        //                    }
        //                case "Size":
        //                    {
        //                        info.EmitterSettings.MinStartSize = float.Parse(node2["MinStartSize"].InnerText);
        //                        info.EmitterSettings.MaxStartSize = float.Parse(node2["MaxStartSize"].InnerText);
        //                        info.EmitterSettings.MinEndSize = float.Parse(node2["MinEndSize"].InnerText);
        //                        info.EmitterSettings.MaxEndSize = float.Parse(node2["MaxEndSize"].InnerText);
        //                        break;
        //                    }
        //                case "Blend":
        //                    {
        //                        info.EmitterSettings.SourceBlend = (Blend)(Enum.Parse(typeof(Blend), node2["SourceBlend"].InnerText));
        //                        info.EmitterSettings.DestinationBlend = (Blend)(Enum.Parse(typeof(Blend), node2["DestinationBlend"].InnerText));
        //                        info.EmitterSettings.AlphaBlendOperation = (BlendFunction)(Enum.Parse(typeof(BlendFunction), node2["AlphaBlendOperation"].InnerText));
        //                        info.EmitterSettings.BlendOperation = (BlendFunction)(Enum.Parse(typeof(BlendFunction), node2["BlendOperation"].InnerText));
        //                        info.EmitterSettings.AlphaFunction = (CompareFunction)(Enum.Parse(typeof(CompareFunction), node2["AlphaFunction"].InnerText));
        //                        break;
        //                    }
        //            }
        //        }

        //        output.Add(info);
        //    }

        //    return output;
        //}

        //private EmitterTypeSettings ParseEmitterTypeSettings(XmlElement node)
        //{
        //    XmlElement type = (XmlElement)node.GetElementsByTagName("EmitterType")[0];
        //    EmitterType et = (EmitterType)Enum.Parse(typeof(EmitterType), type.InnerText);
        //    EmitterTypeSettings settings = new EmitterTypeSettings(et);
        //    switch (et)
        //    {
        //        case EmitterType.Circle:
        //            {
        //                CircleEmitterTypeSettings s = new CircleEmitterTypeSettings(0, false);
        //                s.Radius = float.Parse(node["Radius"].InnerText);
        //                s.RadiateOutward = bool.Parse(node["RadiateOutward"].InnerText);
        //                settings = s;
        //                break;
        //            }
        //        case EmitterType.Line:
        //            {
        //                LineEmitterTypeSettings s = new LineEmitterTypeSettings(0, 0, false);
        //                s.LineLength = float.Parse(node["LineLength"].InnerText);
        //                s.PerpendicularVelocity = bool.Parse(node["PerpendicularVelocity"].InnerText);
        //                s.Rotation = float.Parse(node["Rotation"].InnerText);

        //                settings = s;
        //                break;
        //            }
        //        case EmitterType.Point:
        //            {
        //                //settings = new EmitterTypeSettings(EmitterType.Point);
        //                break;
        //            }
        //        case EmitterType.Rectangle:
        //            {
        //                RectangleEmitterTypeSettings s = new RectangleEmitterTypeSettings(new RectangleF(), 0, false);
        //                RectangleF rec = new RectangleF();

        //                XmlElement el = node["Rectangle"];
        //                rec.X = float.Parse(el.Attributes["X"].Value);
        //                rec.Y = float.Parse(el.Attributes["Y"].Value);
        //                rec.Width = float.Parse(el.Attributes["Width"].Value);
        //                rec.Height = float.Parse(el.Attributes["Height"].Value);

        //                s.Rotation = float.Parse(node["Rotation"].InnerText);
        //                s.RotateVelocity = bool.Parse(node["RotateVelocity"].InnerText);
        //                s.CreateOnEdge = bool.Parse(node["CreateOnEdge"].InnerText);

        //                s.Rectangle = rec;
        //                settings = s;
        //                break;
        //            }
        //    }

        //    return settings;
        //}
#endregion
    }
}